<template>
  <div class="app-container">

    <el-row :gutter="20" class="header">
      <el-col :span="7">
        <el-input placeholder="请输入学生姓名或学号..." v-model="queryForm.query" clearable></el-input>
      </el-col>
      <el-button type="primary" :icon="Search" @click="initScoreList">搜索</el-button>
      <el-button type="success" :icon="DocumentAdd" @click="handleDialogValue()">新增成绩</el-button>
    </el-row>

    <el-table :data="tableData" stripe style="width: 100%">
      <el-table-column type="selection" width="55"/>
      <el-table-column prop="student_id" label="学号" width="120" align="center"/>
      <el-table-column prop="student_name" label="姓名" width="120" align="center"/>
      <el-table-column prop="subject" label="科目" width="120" align="center"/>
      <el-table-column prop="score" label="成绩" width="100" align="center">
        <template v-slot="scope">
          <el-tag :type="getScoreType(scope.row.score)">{{ scope.row.score }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="semester" label="学期" width="150" align="center"/>
      <el-table-column prop="exam_type" label="考试类型" width="120" align="center"/>
      <el-table-column prop="exam_date" label="考试日期" width="150" align="center"/>
      <el-table-column prop="remark" label="备注" min-width="150"/>
      <el-table-column prop="action" label="操作" width="200" fixed="right" align="center">
        <template v-slot="scope">
          <el-button type="primary" :icon="Edit" @click="handleDialogValue(scope.row.id)">编辑</el-button>
          <el-popconfirm title="您确定要删除这条成绩记录吗？" @confirm="handleDelete(scope.row.id)">
            <template #reference>
              <el-button type="danger" :icon="Delete">删除</el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>

    </el-table>
    <el-pagination
        v-model:current-page="queryForm.pageNum"
        v-model:page-size="queryForm.pageSize"
        :page-sizes="[10, 20, 30, 40]"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
    />
    <Dialog v-model="dialogVisible" :dialogVisible="dialogVisible" :id="id" :dialogTitle="dialogTitle"
            @initScoreList="initScoreList"></Dialog>
  </div>
</template>

<script setup>
import requestUtil from '@/util/request'
import {Search, Delete, DocumentAdd, Edit} from '@element-plus/icons-vue'
import {ref} from "vue";
import Dialog from './components/scoreDialog'
import {ElMessage} from "element-plus";

const tableData = ref([])
const total = ref(0)
const queryForm = ref({
  query: '',
  pageNum: 1,
  pageSize: 10
})

const dialogVisible = ref(false)
const dialogTitle = ref("")
const id = ref(-1)

const handleDialogValue = (scoreId) => {
  if (scoreId) {
    id.value = scoreId;
    dialogTitle.value = "编辑成绩"
  } else {
    id.value = -1;
    dialogTitle.value = "新增成绩"
  }
  dialogVisible.value = true
}

// 根据成绩设置标签颜色
const getScoreType = (score) => {
  if (score >= 90) return 'success'
  if (score >= 80) return ''
  if (score >= 60) return 'warning'
  return 'danger'
}

const initScoreList = async () => {
  const res = await requestUtil.get("score/list", queryForm.value)
  tableData.value = res.data.scoreList
  total.value = res.data.total
}

const handleSizeChange = (pageSize) => {
  queryForm.value.pageSize = pageSize
  queryForm.value.pageNum = 1
  initScoreList()
}

const handleCurrentChange = (pageNum) => {
  queryForm.value.pageNum = pageNum
  initScoreList()
}

const handleDelete = async (id) => {
  const res = await requestUtil.post("score/delete", {id})
  if (res.data.code == 200) {
    ElMessage.success("删除成功！")
    initScoreList()
  } else {
    ElMessage.error(res.data.message)
  }
}

initScoreList()

</script>

<style lang="scss" scoped>

.header {
  padding-bottom: 16px;
  box-sizing: border-box;
}

.el-pagination {
  float: right;
  padding: 20px;
  box-sizing: border-box;
}

::v-deep th.el-table__cell {
  word-break: break-word;
  background-color: #f8f8f9 !important;
  color: #515a6e;
  height: 40px;
  font-size: 13px;
}

.el-tag {
  font-weight: bold;
}
</style>
